package com.clw.controller;

import com.clw.dataobject.User;
import com.clw.enums.ResultEnum;
import com.clw.exception.SellException;
import com.clw.form.RegisterForm;
import com.clw.form.UserForm;
import com.clw.service.UserService;
import com.clw.utils.ResultVOUtils;
import com.clw.utils.StringUtils;
import com.clw.vo.ResultVO;
import com.sun.org.apache.xml.internal.security.keys.KeyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 卖家用户
 * 2017-07-30 15:28
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public ModelAndView list(){
        return new ModelAndView("user/index");
    }
    @ResponseBody
    @RequestMapping("/getData")
    public Map<String,Object> getData(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                      HttpServletRequest request) {
        PageRequest request1=new PageRequest(page-1,rows);
        String nickname=request.getParameter("nickname");
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        Page<User> sellerInfos=userService.findAll(nickname,startTime,endTime,request1);
        Map<String,Object> map=new HashMap<>();
        map.put("rows",sellerInfos.getContent());
        map.put("total",userService.getSize(nickname,startTime,endTime));
        return map;
    }

    @RequestMapping("/view")
    public ModelAndView addView(@RequestParam(value = "id",required = false) Integer id,
                                Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            User user=userService.findOne(id);
            map.put("user",user);
        }
        return new ModelAndView("user/add_view");
    }


    @ResponseBody
    @PostMapping("del")
    public ResultVO del(@RequestParam(value = "id",required = false) Integer id){
        User user=userService.findOne(id);
        if (user==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        userService.del(user);
        return ResultVOUtils.success("删除成功");
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultVO save(@Valid UserForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        User user=new User();
        try{
            if (StringUtils.isNotEmpty(form.getId())){
                user=userService.findOne(form.getId());
            }
            BeanUtils.copyProperties(form,user);
            userService.save(user);
        }catch (SellException e){
            return ResultVOUtils.error(ResultEnum.SAVE_ERROR.getCode(),
                    ResultEnum.SAVE_ERROR.getMessage());
        }
        return ResultVOUtils.success("保存成功");
    }

    @RequestMapping("getAllUser")
    @ResponseBody
    public List<User> getAllUser(){
        return userService.findAll();
    }
}
